import cv2
import numpy as np

def l1_distance(image1, image2):
    """
    计算两个图像的 L1 距离

    参数：
    image1, image2: 两个 numpy 数组，表示图像

    返回值：
    distance: L1 距离
    """
    # 确保图像具有相同的形状
    assert image1.shape == image2.shape, "两个图像的形状不匹配"
    
    # 计算 L1 距离
    distance = np.mean(np.abs(image1 - image2))
    
    return distance

def l2_distance(image1, image2):
  """
  计算两个图像的 L2 距离

  参数：
  image1, image2: 两个 numpy 数组，表示图像

  返回值：
  distance: L2 距离
  """
  # 确保图像具有相同的形状
  assert image1.shape == image2.shape, "两个图像的形状不匹配"

  # 计算 L2 距离
  distance = np.sqrt(np.mean((image1 - image2) ** 2))

  return distance

def calculate_mse(image1, image2):
  """
  计算两个图像的均方差（Mean Squared Error, MSE）

  参数：
  image1, image2: 两个图像的 numpy 数组

  返回值：
  mse: 均方差
  """
  # 确保图像具有相同的形状
  assert image1.shape == image2.shape, "两个图像的形状不匹配"

  # 计算 MSE
  mse = np.mean((image1 - image2) ** 2)

  return mse


import cv2
import numpy as np

def detect_shift(image1, image2):
    # Convert images to grayscale
    gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
    gray2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
    
    # Initialize SIFT detector
    sift = cv2.SIFT_create()
    
    # Find keypoints and descriptors
    keypoints1, descriptors1 = sift.detectAndCompute(gray1, None)
    keypoints2, descriptors2 = sift.detectAndCompute(gray2, None)
    
    # Initialize keypoint matcher
    matcher = cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE)
    
    # Match descriptors
    matches = matcher.match(descriptors1, descriptors2)
    
    # Sort matches by distance
    matches = sorted(matches, key=lambda x: x.distance)
    
    # Extract matched keypoints
    matched_keypoints1 = np.array([keypoints1[match.queryIdx].pt for match in matches]).reshape(-1, 1, 2)
    matched_keypoints2 = np.array([keypoints2[match.trainIdx].pt for match in matches]).reshape(-1, 1, 2)
    
    # Estimate affine transformation
    transformation, _ = cv2.estimateAffinePartial2D(matched_keypoints1, matched_keypoints2)
    
    # Extract translation from transformation matrix
    shift_x = transformation[0, 2]
    shift_y = transformation[1, 2]
    
    return shift_x, shift_y

image1 = cv2.imread(r'gym_pih\image\0.jpg')
image2= cv2.imread(r'gym_pih\image\1.jpg')
image3= cv2.imread(r'gym_pih\image\2.jpg')
image4= cv2.imread(r'gym_pih\image\3.jpg')
image5= cv2.imread(r'gym_pih\image\4.jpg')
image6= cv2.imread(r'gym_pih\image\5.jpg')
image7= cv2.imread(r'gym_pih\image\6.jpg')
image8= cv2.imread(r'gym_pih\image\7.jpg')
# image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
# image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)
# image3 = cv2.cvtColor(image3, cv2.COLOR_BGR2GRAY)
# image4 = cv2.cvtColor(image4, cv2.COLOR_BGR2GRAY)
# image5 = cv2.cvtColor(image5, cv2.COLOR_BGR2GRAY)
# image6 = cv2.cvtColor(image6, cv2.COLOR_BGR2GRAY)
# image7 = cv2.cvtColor(image7, cv2.COLOR_BGR2GRAY)
# image8 = cv2.cvtColor(image8, cv2.COLOR_BGR2GRAY)

# distance1 = l1_distance(image1, image2)
# distance2 = l1_distance(image1, image3)
# distance3 = l1_distance(image1, image4)
# distance4 = l1_distance(image1, image5)
# distance5 = l1_distance(image1, image6)
# distance6 = l1_distance(image1, image7)
# distance7 = l1_distance(image1, image8)
# distance8 = l1_distance(image2, image3)

# distance11 = calculate_mse(image1, image2)
# distance22 = calculate_mse(image1, image3)
# distance33 = calculate_mse(image1, image4)
# distance44 = calculate_mse(image1, image5)
# distance55 = calculate_mse(image1, image6)
# distance66 = calculate_mse(image1, image7)
# distance77 = calculate_mse(image1, image8)
# distance88 = calculate_mse(image2, image3)

# print("distance1:", distance1,",distance11",distance11,"\n" ,
# "distance2:", distance2,",distance22",distance22,"\n" ,
# "distance3:", distance3,",distance33",distance33,"\n" ,
# "distance4:", distance4,",distance44",distance44,"\n" ,
# "distance5:", distance5,",distance55",distance55,"\n" ,
# "distance6:", distance6,",distance66",distance66,"\n" ,
# "distance7:", distance7,",distance77",distance77,"\n" ,
# "distance8:", distance8,",distance88",distance88,"\n"
        # )

shift_x1, shift_y1 = detect_shift(image1, image2)
shift_x2, shift_y2 = detect_shift(image1, image3)
shift_x3, shift_y3 = detect_shift(image1, image4)
shift_x4, shift_y4 = detect_shift(image1, image5)
shift_x5, shift_y5 = detect_shift(image1, image6)
shift_x6, shift_y6 = detect_shift(image1, image7)
shift_x7, shift_y7 = detect_shift(image1, image8)

print("shift_x1:", shift_x1,",shift_y1",shift_y1,"\n" ,
"shift_x2:", shift_x2,",shift_y2",shift_y2,"\n" ,
"shift_x3:", shift_x3,",shift_y3",shift_y3,"\n" ,
"shift_x4:", shift_x4,",shift_y4",shift_y4,"\n" ,
"shift_x5:", shift_x5,",shift_y5",shift_y5,"\n" ,
"shift_x6:", shift_x6,",shift_y6",shift_y6,"\n" ,
"shift_x7:", shift_x7,",shift_y7",shift_y7,"\n"
        )


